package com.htdc.web.intercaptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: Sfx
 * Date: 2018/9/4 0004
 * Time: 下午 7:27
 * To change this template use File | Settings | File Templates.
 */
public class Intercaptor implements HandlerInterceptor {

//    拦截器  当没有通过登录认证 无法进入后台的管理操作
    private String[] ignoreList = {"/user/toLogin", "/user/login"};

    @Override
    public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object o) throws Exception {
        String uri = req.getRequestURI();
        HttpSession session = req.getSession();
        if (session.getAttribute("user") == null) {
            boolean flag = false;
            for (String s : ignoreList) {
                if (uri.contains(s)) {
                    return true;
                }

            }
            if (!flag) {
                req.setAttribute("msg", "请先登录再访问!");
                req.getRequestDispatcher("/user/toLogin").forward(req, resp);
                return false;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
